{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tree(Token('RULE', 'start'), [Tree(Token('RULE', 'breast_level'), [Token('LP_ZH', '（'), Token('BIRADS', 'BI-RADS'), Token('COLON_ZH', '：'), Token('DIGIT', '3'), Token('RP_ZH', '）')])])\n"
     ]
    }
   ],
   "source": [
    "from lark import Lark\n",
    "ebnf = r\"\"\"\n",
    "start: breast_level\n",
    "breast_level: LP_ZH BIRADS COLON_ZH DIGIT RP_ZH\n",
    "BIRADS: \"BI-RADS\"\n",
    "LP_ZH: \"（\"\n",
    "RP_ZH: \"）\"\n",
    "COLON_ZH: \"：\"\n",
    "%import common.DIGIT\n",
    "\"\"\"\n",
    "bi_parser = Lark(ebnf)\n",
    "text = '（BI-RADS：3）'\n",
    "tree = bi_parser.parse(text)\n",
    "print(tree)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "    LP: \"（\"\n",
    "    RP: \"）\"\n",
    "    # sentences\n",
    "    # sentences: SENTENCE*\n",
    "    # semicolon_clause: /[^；。]+；/\n",
    "    # sentence_clause: /[]/\n",
    "    # BREAST: \"左乳\"\n",
    "    # SYMPTOM: \"囊肿\"\n",
    "    # NEGATION: \"未见\" | \"无明显\"\n",
    "    text = \"右乳低回声占位（BI-RADS：3）。&#&双乳囊肿（BI-RADS：2）。&#&左乳导管扩张（BI-RADS：2）；&#&双腋下未见明显异常肿大淋巴结。&#&\t\".replace('&#&', '')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tree(Token('RULE', 'start'), [Tree(Token('RULE', 'test'), [Token('A', 'aaa')]), Tree(Token('RULE', 'test'), [Token('A', '123')]), Tree(Token('RULE', 'test'), [Token('A', 'b')])])\n"
     ]
    }
   ],
   "source": [
    "ebnf = r\"\"\"\n",
    "start: test*\n",
    "test: A | B\n",
    "A: /\\w+/\n",
    "B: /\\d+/\n",
    "%import common.WS\n",
    "%ignore WS\n",
    "\"\"\"\n",
    "text = \"aaa  123   b\"\n",
    "parser = Lark(ebnf)\n",
    "tree = parser.parse(text)\n",
    "print(tree)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.6 ('py310')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "49ec5068d4d6d3736c39d3918a16f1ca551a23384109b69898c01c2015d1370c"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
